import socket
import threading
import time

def doPing(scoket):
## 基础数据
    telLen = 0x270f
    # telId = 0x1B5E3 # 心跳
    # telId = 0x1B581 #A3移载机自动确认
    # telId = 0x1B582 #辊道叠钢


    telId = 0x1DC91 #装钢偏斜30.4
    # telId = 0x1DC92 #抽钢偏斜30.4

    #telId = 0x203A1 #装钢偏斜30.5
    #telId = 0x203A2 #抽钢偏斜30.5
    #telId = 0x203A3 #炉门定位30.5

# counter = 0x270f # 心跳字段

    planNo = '120330000000'
    doorNum='123' # 装钢偏斜炉门编号

    state = 0x1

    spare = 0x3
    # heartBeatSpare = 0x270f

    ## 数据转换为bytes
    telLenByte = telLen.to_bytes(4,'big')
    telIdByte = telId.to_bytes(4,'big')
    # planNoByte = planNo.to_bytes(12,'big')
    planNoByte= planNo.encode()
    doorNumByte=doorNum.encode()
    stateByte = state.to_bytes(4,'big')
    spareByte = spare.to_bytes(24,'big')


    s.sendall(telLenByte + telIdByte + planNoByte + stateByte+ spareByte)
    print("--------- socket已发送 -----------")
    data_rec=s.recv(102400)
    print("--------- 接收到服务端推送的数据 -----------")
    print(data_rec.decode('utf-8'))
def ping(scoket):
    while True:
        doPing(scoket)
        time.sleep(30)

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(("127.0.0.1",55201))
s.connect(("127.0.0.1",5001))

# doPing(s)
# time.sleep(1)
# s.close()
# 创建一个线程专门接收服务端数据并且打印
t1 = threading.Thread(target=ping,args=(s,))
t1.start()

